<?php
namespace app\mini_api\controller;

class Article extends Permission
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function getList()
    {
        $page = isset($this->input_data['page']) ? $this->input_data['page'] : 1;
        $pageSize = isset($this->input_data['pageSize']) ? $this->input_data['pageSize'] : 15;
        $map = array();

        if(isset($this->input_data['cate_id']) && $this->input_data['cate_id'] > 0) $map['cate_id'] = $this->input_data['cate_id'];
        $total = db('Article')->where($map)->count();
        if($pageSize == 0)
        {
            $list = db('Article')->order('id', 'desc')->select();
        }
        else
        {
            $list = db('Article')->order('id', 'desc')->page($page,$pageSize)->where($map)->select();
        }
        
        $cate = $this->_getArticleCate();

        $data = [
            'total' => $total,
            'list' => $list,
            'cate' => $cate
        ];
                
        $this->resSuc($data);
    }

    private function _getArticleCate()
    {
        $rows = db('ArticleCate')->order('id DESC')->select();
        $list = [];
        $list[] = [
            'id' => 0,
            'title' => '全部'
        ];

        foreach ($rows as $v) {
            $list[] = $v;
        }
        return $list;
    }

    public function getById()
    {
        $map['id'] = $this->input_data['id'];
        $result = db('Article')->where($map)->find();
        $result['createTime'] = date('Y-m-d',$result['create_time']);

        $this->resSuc($result);
    }


}
